package com.xxh.dasongapp.security.config.handler;

import com.xxh.dasongapp.common.Result;
import com.xxh.dasongapp.common.ResultCode;
import com.xxh.dasongapp.security.utils.ResponseUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Log4j2
public class UserAuthAccessDeniedHandler implements AccessDeniedHandler {
    /**
     * 无权限返回结果
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exception){
        log.info("用户未授权拦截");
        ResponseUtil.renderString(response,Result.error(ResultCode.FORBIDDEN.getCode(),ResultCode.FORBIDDEN.getMsg()));
    }
}
